import {title, tokenName} from '@/config/proj';
import { getUserInfo, login, logout } from "@/api/user";
import {
    getAccessToken,
    removeAccessToken,
    setAccessToken,
} from "@/utils/accessToken";
import {GetterTree, MutationTree, ActionTree} from 'vuex';
import {resetRouter} from '@/router';
import {message as Message, notification as Notify} from 'ant-design-vue';


interface UserState {
    accessToken?: any,
    username: string,
    avatar: string,
    permissions?: any[]
}

const state : UserState = {
    accessToken: getAccessToken(),
    username: '',
    avatar: '',
    permissions: []
}
const getters : GetterTree<any, any> = {
    accessToken: (state) => state.accessToken,
    username: (state) => state.username,
    avatar: (state) => state.avatar,
    permissions: (state) => state.permissions,
};
const mutations : MutationTree<any> = {
    setAccessToken(state, accessToken) {
        state.accessToken = accessToken;
        setAccessToken(accessToken);
    },
    setUsername(state, username) {
        state.username = username;
    },
    setAvatar(state, avatar) {
        state.avatar = avatar;
    },
    setPermissions(state, permissions) {
        state.permissions = permissions;
    },
};
const actions : ActionTree<any, any> = {
    setPermissions({ commit }, permissions) {
        commit("setPermissions", permissions);
    },
    async login({ commit }, userInfo) {
        const { data } = await login(userInfo);
        const accessToken = data[tokenName];
        if (accessToken) {
            commit("setAccessToken", accessToken);
            const hour = new Date().getHours();
            const thisTime =
                hour < 8
                    ? "早上好"
                    : hour <= 11
                    ? "上午好"
                    : hour <= 13
                        ? "中午好"
                        : hour < 18
                            ? "下午好"
                            : "晚上好";
            Notify.success({
                message: `欢迎登录${title}`,
                description: `${thisTime}！`
            })
        } else {
            Message.error(`登录接口异常，未正确返回${tokenName}...`)
        }
    },
    async getUserInfo({ commit, state }) {
        const { data } = await getUserInfo();
        if (!data) {
            Message.error('验证失败，请重新登录...')
            return false;
        }
        let { permissions, username, avatar } = data;
        if (permissions && username && Array.isArray(permissions)) {
            commit("setPermissions", permissions);
            commit("setUsername", username);
            commit("setAvatar", avatar);
            return permissions;
        } else {
            Message.error('用户信息接口异常')
            return false;
        }
    },
    async logout({ dispatch }) {
        await logout();
        await dispatch("resetAccessToken");
        await resetRouter();

    },
    resetAccessToken({ commit }) {
        commit("setPermissions", []);
        commit("setAccessToken", "");
        removeAccessToken();
    },
};
export default { state, getters, mutations, actions };


